package com.git.hui.yinshua.biz.rest.admin.goods;

import com.git.hui.yinshua.api.model.ExceptionUtil;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.Status;
import com.git.hui.yinshua.api.model.permission.Permission;
import com.git.hui.yinshua.api.model.permission.UserAccessRole;
import com.git.hui.yinshua.biz.goods.service.AttributeService;
import com.git.hui.yinshua.biz.rest.models.goods.req.AttrKeyReq;
import com.git.hui.yinshua.biz.rest.models.goods.req.AttrValueReq;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.AttrKeyDto;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.AttrValDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author YiHui
 * @date 2024/6/13
 */
@RestController
@RequestMapping(path = "admin/attr")
@Permission(role = UserAccessRole.ADMIN)
public class AdminAttributeController {
    @Autowired
    private AttributeService attributeService;

    @RequestMapping(path = "addKey")
    public ResVo<AttrKeyDto> addKey(@RequestBody AttrKeyReq req) {
        if (StringUtils.isBlank(req.getKey())) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "属性名不能为空");
        }

        req.setKey(req.getKey().trim());
        if (req.getUnit() != null) {
            req.setUnit(req.getUnit().trim());
        }

        return ResVo.ok(attributeService.addKey(req));
    }

    @RequestMapping(path = "updateKey")
    public ResVo<AttrKeyDto> updateKey(@RequestBody AttrKeyReq req) {
        if (req.getKeyId() == null || (req.getKey() != null && StringUtils.isBlank(req.getKey()))) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "请指定需要更新的属性");
        }

        if (req.getCid() == null && req.getUnit() == null && req.getKey() == null) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "请设置需要更新的属性内容");
        }
        return ResVo.ok(attributeService.updateKey(req));
    }

    @RequestMapping(path = "rmKey")
    public ResVo<Boolean> rmKey(@RequestParam("keyId") Integer keyId) {
        return ResVo.ok(attributeService.deleteKey(keyId));
    }

    @RequestMapping(path = "listKey")
    public ResVo<List<AttrKeyDto>> listKeys(@RequestParam(name = "cid", required = false) Integer cid) {
        if (cid != null && cid < 0) {
            cid = 0;
        }
        return ResVo.ok(attributeService.listKeys(cid));
    }

    @RequestMapping(path = "addValue")
    public ResVo<AttrValDto> addVal(@RequestBody AttrValueReq req) {
        if (req.getKeyId() == null || StringUtils.isBlank(req.getValue())) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "请设置需要添加的属性");
        }

        return ResVo.ok(attributeService.addVal(req));
    }

    @RequestMapping(path = "updateValue")
    public ResVo<Boolean> updateVal(@RequestBody AttrValueReq req) {
        if (req.getKeyId() == null || StringUtils.isBlank(req.getValue())) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "请设置需要添加的属性");
        }

        return ResVo.ok(attributeService.updateVal(req));
    }

    @RequestMapping(path = "rmValue")
    public ResVo<Boolean> removeVal(@RequestParam("valId") Integer valId) {
        return ResVo.ok(attributeService.deleteVal(valId));
    }

    @RequestMapping(path = "listValue")
    public ResVo<List<AttrValDto>> listValues(@RequestParam(name = "keyId") Integer keyId) {
        return ResVo.ok(attributeService.listVals(keyId));
    }

}
